package cn.meizhu.pet.controller;

import cn.meizhu.org.domain.Department;
import cn.meizhu.org.query.DepartmentQuery;
import cn.meizhu.pet.domain.Pet;
import cn.meizhu.pet.query.PetQuery;
import cn.meizhu.pet.service.IPetService;
import cn.meizhu.util.AjaxResult;
import cn.meizhu.util.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {

    @Autowired
    private IPetService petService;

    @PostMapping
    public QueryResult<Pet> queryPage(@RequestBody PetQuery petQuery){
        return  petService.queryPage(petQuery);
    }

    @PostMapping("/onsale")
    public AjaxResult onsale(@RequestBody List<Long> list){
        try {
            petService.onsale(list);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }

    @PostMapping("/offsale")
    public AjaxResult offsale(@RequestBody List<Long> list){

        try {
            petService.offsale(list);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }

    @PutMapping
    public AjaxResult save(@RequestBody Pet pet, HttpServletRequest request){
        try {
            petService.save(pet,request);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }


    @PostMapping("/detail/{id}")
    public Pet detail(@PathVariable("id") Long id){
        System.out.println(id);
       return petService.findByIdDetail(id);

    }


}
